package com.example.demo.msg.aio.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.util.concurrent.CountDownLatch;

public class AioServerHandle implements  Runnable {
    public CountDownLatch latch;

    public AsynchronousServerSocketChannel serverSocketChannel;

    public AioServerHandle(int port) {

        try {
            serverSocketChannel = AsynchronousServerSocketChannel.open();

            serverSocketChannel.bind(new InetSocketAddress(port));

            System.out.println("服务器已启动，端口号：" + port);

        } catch (IOException e) {

        }
    }

    @Override
    public void run() {
        latch = new CountDownLatch(1);

        serverSocketChannel.accept(this,new AcceptHandle());

        try {
            latch.await();
        } catch (InterruptedException e) {

        }

    }
}